import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useProjectStore = defineStore('project', () => {
  // 状态
  const projects = ref([])
  const currentProject = ref(null)
  const loading = ref(false)

  // 计算属性
  const activeProjects = computed(() => {
    return projects.value.filter(project => project.status === 'active')
  })

  const totalProjects = computed(() => projects.value.length)

  const projectsWithStats = computed(() => {
    return projects.value.map(project => ({
      ...project,
      documentCount: project.documents?.length || 0
    }))
  })

  // 方法
  const initializeMockData = () => {
    projects.value = [
      {
        id: 1,
        name: "MMC项目",
        fullName: "国家标准化代谢性疾病管理中心",
        color: "#409EFF",
        description: "国家标准化代谢性疾病管理中心建设项目，涉及医疗设备采购、软件平台部署和人员培训。",
        status: "active",
        priority: "high",
        startDate: "2024-01-15",
        endDate: "2024-12-31",
        progress: 65,
        memberCount: 8,
        budget: 2800000,
        manager: "李主任",
        department: "医疗信息化部",
        tags: ["医疗设备", "信息化", "国家项目"],
        createTime: new Date(Date.now() - 180 * 24 * 60 * 60 * 1000).toISOString(),
        updateTime: new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(),
        documents: [1, 5, 6] // 关联文档ID
      },
      {
        id: 2,
        name: "设备采购",
        fullName: "2024年度医疗设备采购项目",
        color: "#67C23A",
        description: "2024年度医疗设备集中采购项目管理，包含血压计、呼吸设备等多种医疗器械。",
        status: "active",
        priority: "medium",
        startDate: "2024-03-01",
        endDate: "2024-08-30",
        progress: 80,
        memberCount: 5,
        budget: 1500000,
        manager: "王经理",
        department: "设备采购部",
        tags: ["设备采购", "医疗器械", "招标"],
        createTime: new Date(Date.now() - 120 * 24 * 60 * 60 * 1000).toISOString(),
        updateTime: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(),
        documents: [2, 7, 8] // 关联文档ID
      },
      {
        id: 3,
        name: "培训资料",
        fullName: "员工培训和考核资料管理",
        color: "#E6A23C",
        description: "员工培训和考核资料管理项目，建立完善的培训体系和考核标准。",
        status: "active",
        priority: "low",
        startDate: "2023-09-01",
        endDate: "2024-09-30",
        progress: 90,
        memberCount: 12,
        budget: 300000,
        manager: "张主管",
        department: "人力资源部",
        tags: ["培训", "考核", "人力资源"],
        createTime: new Date(Date.now() - 300 * 24 * 60 * 60 * 1000).toISOString(),
        updateTime: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString(),
        documents: [3, 4] // 关联文档ID
      },
      {
        id: 4,
        name: "系统集成",
        fullName: "医院信息系统集成优化",
        color: "#F56C6C",
        description: "医院现有信息系统集成优化项目，提升系统间数据交换效率。",
        status: "planning",
        priority: "high",
        startDate: "2024-09-01",
        endDate: "2025-03-31",
        progress: 10,
        memberCount: 6,
        budget: 1200000,
        manager: "陈工程师",
        department: "信息技术部",
        tags: ["系统集成", "信息化", "优化"],
        createTime: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(),
        updateTime: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString(),
        documents: [] // 关联文档ID
      }
    ]
  }

  const getProjectById = (id) => {
    return projects.value.find(project => project.id === id)
  }

  const getProjectDocuments = (projectId) => {
    const project = getProjectById(projectId)
    return project ? project.documents : []
  }

  const addProject = (project) => {
    const newProject = {
      ...project,
      id: Date.now(),
      createTime: new Date().toISOString(),
      updateTime: new Date().toISOString(),
      documents: [],
      progress: 0
    }
    projects.value.unshift(newProject)
    return newProject
  }

  const updateProject = (id, updates) => {
    const index = projects.value.findIndex(project => project.id === id)
    if (index !== -1) {
      projects.value[index] = {
        ...projects.value[index],
        ...updates,
        updateTime: new Date().toISOString()
      }
    }
  }

  const deleteProject = (id) => {
    const index = projects.value.findIndex(project => project.id === id)
    if (index !== -1) {
      projects.value.splice(index, 1)
    }
  }

  const addDocumentToProject = (projectId, documentId) => {
    const project = getProjectById(projectId)
    if (project && !project.documents.includes(documentId)) {
      project.documents.push(documentId)
      project.updateTime = new Date().toISOString()
    }
  }

  const removeDocumentFromProject = (projectId, documentId) => {
    const project = getProjectById(projectId)
    if (project) {
      const index = project.documents.indexOf(documentId)
      if (index !== -1) {
        project.documents.splice(index, 1)
        project.updateTime = new Date().toISOString()
      }
    }
  }

  const getProjectStats = () => {
    return {
      total: projects.value.length,
      active: activeProjects.value.length,
      planning: projects.value.filter(p => p.status === 'planning').length,
      completed: projects.value.filter(p => p.status === 'completed').length,
      totalBudget: projects.value.reduce((sum, p) => sum + (p.budget || 0), 0),
      totalMembers: projects.value.reduce((sum, p) => sum + (p.memberCount || 0), 0)
    }
  }

  return {
    // 状态
    projects,
    currentProject,
    loading,
    
    // 计算属性
    activeProjects,
    totalProjects,
    projectsWithStats,
    
    // 方法
    initializeMockData,
    getProjectById,
    getProjectDocuments,
    addProject,
    updateProject,
    deleteProject,
    addDocumentToProject,
    removeDocumentFromProject,
    getProjectStats
  }
})